home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / language / harvest.cpt / Harvest C / Tcl 6.2 / CTclPane.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-16  |  4.6 KB  |  183 lines

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /******************************************************************************
  30.     CTclPane.c
  31.     
  32.     Copyright 1992 Eric W. Sink        
  33.     Copyright ⌐ 1989 Symantec Corporation. All rights reserved.
  34.  
  35.  ******************************************************************************/
  36.  
  37.  
  38. #include "CTclPane.h"
  39. #include <Commands.h>
  40. #include <CDocument.h>
  41. #include <CBartender.h>
  42. #include <Constants.h>
  43. #include "tcl.h"
  44.  
  45. extern    CBartender    *gBartender;
  46.  
  47. void CTclPane::ITclPane(CView *anEnclosure, CBureaucrat *aSupervisor, Tcl_Interp *anInterp)
  48.  
  49. {
  50.     Rect    margin;
  51.  
  52.     myInterp = anInterp;
  53.  
  54.     CEditText::IEditText(anEnclosure, aSupervisor, 1, 1, 0, 0,
  55.                         sizELASTIC, sizELASTIC, 432);
  56.     FitToEnclosure(TRUE, TRUE);
  57.  
  58.     SetRect(&margin, 2, 2, -2, -2);
  59.     ChangeSize(&margin, FALSE);
  60. }
  61.  
  62. void CTclPane::DoCommand(long theCommand)
  63.  
  64. {
  65.     
  66.     /* I don't support any commands here CUSTOM */
  67.  
  68.     inherited::DoCommand(theCommand);
  69. }
  70.  
  71. /* This is a hack.  If this had been a serious, clean routine, the information
  72. signal you just saw would have been followed by a routine which was much more
  73. well thought out than this one.  This is only a hack. */
  74. void strip_cr(char *s)
  75. {
  76.     int len;
  77.     len = strlen(s);
  78.     if (s[len-1] == '\r') {
  79.         s[len-1] = 0;
  80.     }
  81. }
  82.  
  83. void CTclPane::DoKeyDown(char theChar, Byte keyCode, EventRecord *macEvent)
  84.  
  85. {
  86.     /* The meat of my shell is here.  Basically, this Tcl Shell executes the
  87.     selected text or the current line whenever the enter key is pressed.
  88.     The code below seems to work ok for me - please report any bugs.
  89.     */
  90.  
  91.     char *text;
  92.     if (theChar == kEnterKey) {
  93.         long selStart;
  94.         long selEnd;
  95.         int code;
  96.         int line;
  97.         int len;
  98.         GetSelection(&selStart,&selEnd);
  99.         if (selStart == selEnd) {
  100.             line = FindLine(selEnd);
  101.             selStart = ((**macTE).lineStarts)[line];
  102.             selEnd = ((**macTE).lineStarts)[line+1];
  103.         }
  104.         text = malloc(selEnd-selStart+2);
  105.         strncpy(text,((*(**macTE).hText)+selStart),(selEnd-selStart));
  106.         text[selEnd-selStart] = 0;
  107.         strip_cr(text);
  108.         code = Tcl_Eval(myInterp,text,0,0);
  109.         GetSelection(&selStart,&selEnd);
  110.         line = FindLine(selEnd);
  111.         if (line >= GetNumLines()) {
  112.             selStart = selEnd = (**macTE).teLength;
  113.         }
  114.         else {
  115.             selStart = selEnd = ((**macTE).lineStarts)[line+1];
  116.             if (!selStart) {
  117.                 selStart = selEnd = (**macTE).teLength;
  118.             }
  119.         }
  120.         SetSelection(selEnd,selEnd,TRUE);
  121.         
  122.         len = strlen(myInterp->result);
  123.         
  124.         /* CUSTOM I have chosen to insert the "result" of each command
  125.             into the shell.  You may not want to do this.  Tickle does
  126.             not, for example.*/
  127.         InsertTextPtr("\r",1,FALSE);
  128.         InsertTextPtr(myInterp->result,len,FALSE);
  129.         InsertTextPtr("\r",1,TRUE);
  130.  
  131.         ScrollToSelection();
  132.         
  133.         if (code) SysBeep(1);
  134.         free(text);
  135.         InitCursor();
  136.     }
  137.     else {
  138.         inherited::DoKeyDown(theChar, keyCode, macEvent);
  139.     }
  140.     
  141.     switch (keyCode) {
  142.     
  143.         case KeyHome:
  144.         case KeyEnd:
  145.         case KeyPageUp:
  146.         case KeyPageDown:
  147.             break;
  148.             
  149.         default:    
  150.             if (!((CDocument *)itsSupervisor)->dirty) {
  151.                 ((CDocument *)itsSupervisor)->dirty = TRUE;
  152.                 gBartender->EnableCmd(cmdSave);
  153.                 gBartender->EnableCmd(cmdSaveAs);
  154.             }
  155.             break;
  156.     }
  157. }
  158.  
  159.  
  160. void CTclPane::DoAutoKey(char theChar, Byte keyCode, EventRecord *macEvent)
  161.  
  162. {
  163.     
  164.     inherited::DoAutoKey(theChar, keyCode, macEvent);
  165.  
  166.     switch (keyCode) {
  167.     
  168.         case KeyHome:
  169.         case KeyEnd:
  170.         case KeyPageUp:
  171.         case KeyPageDown:
  172.             break;
  173.             
  174.         default:    
  175.             if (!((CDocument *)itsSupervisor)->dirty) {
  176.                 ((CDocument *)itsSupervisor)->dirty = TRUE;
  177.                 gBartender->EnableCmd(cmdSave);
  178.                 gBartender->EnableCmd(cmdSaveAs);
  179.             }
  180.             break;
  181.     }
  182. }
  183.